package com.wv.api.service.basic.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import com.github.pagehelper.PageHelper;
import com.wv.api.mapper.basic.BasicHouseDao;
import com.wv.api.service.basic.IBasicHouseService;
import com.wv.common.exception.WVException;
import com.wv.common.result.HttpCodeEnum;
import com.wv.common.result.PageResponseResult;
import com.wv.common.result.Result;
import com.wv.model.basic.BasicHouse;
import com.wv.model.basic.condition.BasicHouseCondition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class BasicHouseServiceImpl implements IBasicHouseService {


    @Autowired
    private BasicHouseDao basicHouseDao;

    @Override
    public Result getHouseList(BasicHouseCondition condition) {
        PageHelper.startPage(condition.getPage(), condition.getSize());
        PageResponseResult build = PageResponseResult.builder()
                .currentPage(condition.getPage())
                .total(basicHouseDao.selectHouseCount(condition))
                .build();
        build.setData(basicHouseDao.selectHouse(condition));
        return build;
    }

    @Override
    public Result getAll() {
        return Result.ok(basicHouseDao.selectList(null));
    }

    @Override
    public Result addHouse(BasicHouse basicHouse) {
        basicHouse.setId(IdUtil.createSnowflake(1,1).nextIdStr());
        basicHouse.setCreateUserId(StpUtil.getLoginId().toString());
        basicHouse.setCreateTime(LocalDateTime.now());
        int insert = basicHouseDao.insert(basicHouse);
        return insert > 0 ? Result.ok() : Result.error(HttpCodeEnum.ADD_ERROR);
    }

    @Override
    @Transactional
    public Result addHouse(List<BasicHouse> list) {
        List<BasicHouse> collect = list.stream().peek(item -> {
            String randomStr = String.valueOf(RandomUtil.randomInt(10000,99999));
            item.setId(IdUtil.createSnowflake(1, 1).nextIdStr() + randomStr);
            item.setCreateUserId(StpUtil.getLoginId().toString());
            item.setCreateTime(LocalDateTime.now());
        }).collect(Collectors.toList());
        int insertBatch = basicHouseDao.insertBatch(collect);
        if (insertBatch != collect.size()) throw new WVException(HttpCodeEnum.ADD_ERROR);
        return Result.ok();
    }

    @Override
    @Transactional
    public Result updateHouse(BasicHouse basicHouse) {
        int i = basicHouseDao.updateById(basicHouse);
        if (i == 0) throw new WVException(HttpCodeEnum.UPDATE_ERROR);
        return Result.ok();
    }

    @Override
    @Transactional
    public Result deleteHouse(String id) {
        if (basicHouseDao.deleteById(id) == 0) throw new WVException(HttpCodeEnum.DElETE_ERROR);
        return Result.ok();
    }

    @Override
    @Transactional
    public Result deleteHouse(List<String> ids) {
        if (basicHouseDao.deleteBatchIds(ids) != ids.size()) throw new WVException(HttpCodeEnum.DElETE_ERROR);
        return Result.ok();
    }
}
